/*
 * @Author: Troubadour 2276791354@qq.com
 * @Date: 2024-08-18 10:37:36
 * @LastEditors: Troubadour 2276791354@qq.com
 * @LastEditTime: 2024-08-18 10:59:59
 * @Version:
 * @Description:
 */

/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Include file                                                                       -
--------------------------------------------------------------------------------------------------------------------------------------*/
#include "bsp_gpio.h"
#include "bsp_type.h"
#include "gpio.h"

#include "cola_device.h"
#include "cola_init.h"
/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal macro                                                                     -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal enum                                                                      -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal structure                                                                 -
--------------------------------------------------------------------------------------------------------------------------------------*/

static cola_device_t bsp_spio_dev;
/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal function                                                                  -
--------------------------------------------------------------------------------------------------------------------------------------*/

static void bsp_gpio_register(void);
static int bsp_gpio_init(cola_device_t *dev);
static int bsp_gpio_control(cola_device_t *dev, int cmd, void *args);
/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Internal variables                                                                 -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  External variables                                                                 -
--------------------------------------------------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------------------------------------------------
-                                                  Function body                                                                      -
--------------------------------------------------------------------------------------------------------------------------------------*/

static void bsp_gpio_register(void)
{
    static struct cola_device_ops bsp_gpio_ops = {
        .init = bsp_gpio_init,
        .control = bsp_gpio_control,
    };

    bsp_spio_dev.dops = &bsp_gpio_ops;
    bsp_spio_dev.name = "bsp_gpio";
    bsp_spio_dev.owner = NULL;
    cola_device_register(&bsp_spio_dev);
}
device_initcall(bsp_gpio_register);

static int bsp_gpio_init(cola_device_t *dev)
{
    MX_GPIO_Init();
    return 0;
}

static int bsp_gpio_control(cola_device_t *dev, int cmd, void *args)
{
    uint8_t pin_state = *(uint8_t *)args;

    switch (cmd)
    {
    case EN_GPIO_CTRL_CS:
        HAL_GPIO_WritePin(TFT_CS_GPIO_Port, TFT_CS_Pin, (pin_state) ? (GPIO_PIN_SET) : (GPIO_PIN_RESET));
        break;

    case EN_GPIO_CTRL_DC:
        HAL_GPIO_WritePin(TFT_DC_GPIO_Port, TFT_DC_Pin, (pin_state) ? (GPIO_PIN_SET) : (GPIO_PIN_RESET));
        break;

    case EN_GPIO_CTRL_RST:
        HAL_GPIO_WritePin(TFT_RST_GPIO_Port, TFT_RST_Pin, (pin_state) ? (GPIO_PIN_SET) : (GPIO_PIN_RESET));
        break;

    case EN_GPIO_CTRL_BK:
        HAL_GPIO_WritePin(TFT_BLK_GPIO_Port, TFT_BLK_Pin, (pin_state) ? (GPIO_PIN_SET) : (GPIO_PIN_RESET));
        break;

    default:
        return 1;
    }
    return 0;
}








